package com.ysxsoft.wdg.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.my.TxHisAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.HomeProductResponse;
import com.ysxsoft.wdg.bean.response.TxHistoryResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

public class TxHisActivity extends BaseActivity {


    public static void start(Context context){
        Intent intent = new Intent(context,TxHisActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;


    private TxHisAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tx_his;
    }

    @Override
    protected void initData() {
        initStatusBar(topView,false);
        titleContent.setText("提现记录");
        tv0.setSelected(true);

        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new TxHisAdapter(R.layout.item_tx_his);
        recyclerview.setAdapter(adapter);

        getList();
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                getList();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                getList();
            }
        });
    }


    @OnClick({R.id.title_finish,R.id.tv0, R.id.tv1, R.id.tv2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.tv0:
                tv0.setSelected(true);
                tv1.setSelected(false);
                tv2.setSelected(false);
                type=2;
                page=1;
                getList();
                break;
            case R.id.tv1:
                tv0.setSelected(false);
                tv1.setSelected(true);
                tv2.setSelected(false);
                type=0;
                page=1;
                getList();
                break;
            case R.id.tv2:
                tv0.setSelected(false);
                tv1.setSelected(false);
                tv2.setSelected(true);
                type=1;
                page=1;
                getList();
                break;
        }
    }


    @OnClick()
    public void onViewClicked() {

    }

    private int page = 1;
    private int type=2;
    /**
     * 获取提现历史记录
     */
    private void getList() {
        OkHttpUtils.post()
                .url(UrlHost.TX_LOG)
                .addParams("uid",  SharePrefUtils.getUserId())
                .addParams("type", type + "")//	0审核中  1已完成 2全部
                .addParams("page", page+"")//	0审核中  1已完成 2全部
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    TxHistoryResponse txHistoryResponse = JsonUtils.parseByGson(response, TxHistoryResponse.class);
                    if (txHistoryResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(txHistoryResponse.getCode())) {
                        List<TxHistoryResponse.DataBean> list = txHistoryResponse.getData();
                        if (page == 1) {
                            multStatusView.showContent();
                            if (list != null && !list.isEmpty()) {
                            } else {
                                multStatusView.showEmpty();
                            }
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                    }else{
                        if(page==1){
                            multStatusView.showEmpty();
                        }
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }

}
